/**
 * Created by JustinChow on 2016/11/4.
 */

"use strict";

const Util = require("util");
const GameObject = require("./GameObject");
const Vector = require("../Utils/Vector");

const StaticTankConfig = require("../Configs/Datas/Tank.json");

function Player(objectId, owner, position, gameServer, staticId) {
    GameObject.apply(this, Array.prototype.slice.call(arguments));

    this.type = 0;
    this.staticId = staticId;
    this.staticConfig = this.loadConfig();

    this.isBot = false;

    this.initCollision();
}
Util.inherits(Player, GameObject);

module.exports = Player;

Player.prototype.sendUpdate = function () {
    return true;
};

Player.prototype.onAdd = function () {

};

Player.prototype.onRemove = function () {

};

Player.prototype.move = function () {
    this.position.add(this.getTankSpeedValue());

    // Only For Debug Usage
    // if (this.position.x < this.gameServer.config.borderLeft
    //     || this.position.x > this.gameServer.config.borderRight
    //     || this.position.y < this.gameServer.config.borderTop
    //     || this.position.y > this.gameServer.config.borderBottom) {
    //     this.position.x = ( this.gameServer.config.borderRight - this.gameServer.config.borderLeft ) / 2;
    //     this.position.y = ( this.gameServer.config.borderBottom - this.gameServer.config.borderTop ) / 2;
    // }

    if (this.position.x < this.gameServer.config.borderLeft) this.position.x = this.gameServer.config.borderLeft;
    else if (this.position.x > this.gameServer.config.borderRight) this.position.x = this.gameServer.config.borderRight;

    if (this.position.y < this.gameServer.config.borderTop) this.position.y = this.gameServer.config.borderTop;
    else if (this.position.y > this.gameServer.config.borderBottom) this.position.y = this.gameServer.config.borderBottom;
};

Player.prototype.loadConfig = function () {
    if (!StaticTankConfig[this.staticId]) return {};
    return StaticTankConfig[this.staticId];
};

/**
 * Calc Speed Value Base On Speed Per Second And Time Tick Rate
 * @returns {number}
 */
Player.prototype.getTankSpeedValue = function () {
    var scale =  this.gameServer.config.tankSpeed / (1e3 / this.gameServer.config.ticks / this.gameServer.config.fullTicks);
    return this.owner.speed.clone().scale(scale);
};